package com.ofhb.webapp.server.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ejb.EJB;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.ofhb.bean.DataReaderBean;
import com.ofhb.entities.UserEntity;
import com.ofhb.entities.UserTypeEntity;
import com.ofhb.webapp.client.services.DataReaderService;
import com.ofhb.webapp.server.utils.OfhbUtils;
import com.ofhb.webapp.shared.dto.UserDTO;
import com.ofhb.webapp.shared.dto.UserTypeDTO;

public class DataReaderServiceImpl extends RemoteServiceServlet implements DataReaderService {

    private static final long serialVersionUID = -8241469346391765309L;
    
    private final Logger logger = Logger.getLogger(DataReaderServiceImpl.class.getName());

    @EJB
    DataReaderBean dataReaderBean;
    
    @Override
    public List<UserTypeDTO> getUserTypes() throws IllegalArgumentException {
	List<UserTypeDTO> userTypeDTOs = new ArrayList<UserTypeDTO>();
	List<UserTypeEntity> userTypeEntities = dataReaderBean.getAllUserTypes();
	for (UserTypeEntity userTypeEntity : userTypeEntities) {
	    UserTypeDTO userTypeDTO = new UserTypeDTO();
	    OfhbUtils.copyClass(userTypeEntity,userTypeDTO);
	    userTypeDTOs.add(userTypeDTO);
	}
	return userTypeDTOs;
    }

	@Override
	public Boolean isUserNameExists(String userName) throws IllegalArgumentException {
		Boolean result = false;
		try {
			result = dataReaderBean.isUserNameExists(userName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public List<UserDTO> getHallOwners() throws IllegalArgumentException {
		logger.log(Level.INFO, "[GWT:::LOG] : getHallOwners() - START");
		List<UserDTO> result = null;
		try {
			List<UserEntity> userEntities = dataReaderBean.getAllOwners();
			if(userEntities.size() > 0) {
				result = new ArrayList<UserDTO>();
				for (UserEntity userEntity : userEntities) {
					UserDTO userDTO = new UserDTO();
					OfhbUtils.copyClass(userEntity, userDTO);
					result.add(userDTO);
				}
			}
			logger.log(Level.INFO, "[GWT:::LOG] : getHallOwners() - DONE");
		} catch (Exception e) {
			logger.log(Level.INFO, "[GWT:::LOG] : getHallOwners() - Exception");
			e.printStackTrace();
		}
		return result;
	}
}
